<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Feedback extends Controller {
	var $table='feedback';
	var $view='feedback';
	var $rule_post=array(
		array(
			'field'=>'type_id',
			'rules'=>'intval'),
		array(
			'field'=>'username',
			'label'=>'姓名',
			'rules'=>'trim|max_length[50]|xss_clean'),
		array(
			'field'=>'tel',
			'label'=>'电话',
			'rules'=>'required|xss_clean'),
		array(
			'field'=>'addr',
			'label'=>'传真',
			'rules'=>'trim|xss_clean'),
		array(
			'field'=>'fax',
			'label'=>'传真',
			'rules'=>'required|xss_clean'),
		array(
			'field'=>'email',
			'label'=>'邮箱',
			'rules'=>'required|valid_email|xss_clean'),
		array(
			'field'=>'content',
			'label'=>'内容',
			'rules'=>'trim|max_length[1000]|xss_clean'),
	array(
			'field'=>'captcha',
			'label'=>'验证码',
			'rules'=>'trim|required|callback_check_captcha'),	
		);
		
	function index(){
		$this->load->helper('form');
		$this->load->view($this->view,get_defined_vars());
	}
	
	function post(){
		$this->load->library('form_validation',$this->rule_post);
		if(!$this->form_validation->run()){
			return $this->index();
		}
		$form=$this->form_validation->to_array();
		$form['timeline']=time();
		$form['type_id']=$this->uri->segment(3,-1);
		unset($form['captcha']);
		$this->load->database();
		if(!$this->db->insert($this->table,$form)){
			$this->form_validation->set_error('username'
				,$this->lang->line('item_put_failure'));
			return $this->index();
		}
		goto_message($this->lang->line('site_post_success'),'index');
	}
	
	function check_captcha($s){
		$this->load->model('m_captcha');
		if($this->m_captcha->verify($s))
		{
			return true;
		}
		else
		{
			$this->form_validation->set_message('check_captcha','验证码错误');
			return false;
		}
		echo js_encode($s);
	}	
}